package com.xframe.common.core.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.Date;

@ApiModel(value = "通用响应对象")
public class ResultDTO<T> implements Serializable {

    private static final long serialVersionUID = 10L;

    /**
     * 编码：200表示成功，其他值表示失败
     */
    @ApiModelProperty(value = "编码：200表示成功，非0值表示失败")
    private int code = 200;
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    private String msg = "success";
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    @ApiModelProperty(value = "响应数据")
    private Long timeStamp;

    public ResultDTO<T> setResult(T data) {
        this.setData(data);
        this.timeStamp = new Date().getTime();
        return this;
    }

    public static <T> ResultDTO<T>  success(T data){
        ResultDTO<T> result = new ResultDTO<>();
        result.setCode(200);
        result.setData(data);
        result.setMsg("");
        result.timeStamp = new Date().getTime();
        return result;
    }

    public ResultDTO<T> error(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.timeStamp = new Date().getTime();
        return this;
    }

    public ResultDTO<T> error(String msg) {
        this.code = ErrorCode.INTERNAL_SERVER_ERROR;
        this.msg = msg;
        this.timeStamp = new Date().getTime();
        return this;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getTimeStamp() {
        return timeStamp;
    }

}
